<?php

class Branch extends Controller {
	
	/**
	 * Constructor
	 */
	function Branch(){
		parent::Controller();
		$this->load->model('Branch_model');
		$this->load->model('Relations_model');
	}
	
	/**
	 * Add a new branch to the database
	 */
	function add_branch($pkgid){
		if (is_numeric($pkgid) && $this->auth->has_rights_to("add","bch",$pkgid))
		{
			$infos = array(	'name' => $this->input->post('name'),
							'description' => $this->input->post('description'));
			
			if (! file_exists($this->config->item('download').$pkgid) ){
				$this->load->view('forbidden');
			}
			else 
			{
				$result=$this->Branch_model->new_branch($infos, $pkgid);
				if($result===true){
					$data = $this->Branch_model->data;
					$path = $this->config->item('download').$pkgid.'/'.$data['branch_id'];
					if (!mkdir($path))
						echo "can't create path";
					$this->load->view('branch/addOk',$infos);
				}
				else{
					$param['values'] = array();
					foreach($infos as $key => $value){
						if(isset($result[$key])){
							$param['values'][$key.'_val'] = '';
						}else{
							$param['values'][$key.'_val'] = $value;
						}
					}
					
					$param['errors'] = array();
					foreach($infos as $key => $value){
						$param['errors'][$key.'_err'] = '';
					}
					foreach($result as $key => $value){
						$param['errors'][$key.'_err'] = $value;
					}
					
					$param['pkgid'] = $pkgid;
					$this->load->view('branch/add', $param);
				}
			}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the add package form
	 */
	function add($pkgid){
		if (is_numeric($pkgid) && $this->auth->has_rights_to("add","bch",$pkgid))
		{
			$param['values'] = array('name_val' => '', 'description_val' => '');
			$param['errors'] = array('name_err' => '', 'description_err' => '');
			$param['pkgid']=$pkgid;
			
			$this->load->view('branch/add',$param);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the information of a branch
	 */
	function load($branch_id){
		if (is_numeric($branch_id) && $this->auth->has_rights_to("lookat","bch",$branch_id))
		{
			$this->Branch_model->load_branch_from_id($branch_id);
			$data = $this->Branch_model->data;
			if(is_array($data))
				$this->load->view('branch/show',$data);
			else
				$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the information of the branch
	 */
	function loadBranch($branch_id){
		if (is_numeric($branch_id) && $this->auth->has_rights_to("lookat","bch",$branch_id))
		{
			$data['branchlist'] = $this->Branch_model->get_branches($branch_id, null, null, array('status' => 'active'));
			if(is_array($data))
				$this->load->view('branch/list',$data);
			else
				$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	
	
	/**
	 * Display the branch list
	 */
	function branchlist($pkgid){	
		if (is_numeric($pkgid) && $this->auth->has_rights_to("list","bch"))
		{
			$this->load->model('Branch_model');	
			$data['branchlist']=$this->Branch_model->get_branches($pkgid, null, null, array('status' => 'active'));	
			if (is_array($data['branchlist']))
				$this->load->view('branch/list',$data);
			else
				$this->load->view('system_error');	
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Deletes the branch
	 */
	function delete($bch_id) {
		if (is_numeric($bch_id) && $this->auth->has_rights_to("delete","bch", $bch_id))
		{
			if (!$this->Branch_model->save_branch($bch_id, array('status' => 'deleted')))
				$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the edit branch view in order to edit the branch $id
	 */
	function edit($id){
		
		if (is_numeric($id) && $this->auth->has_rights_to("edit","bch", $id))
		{
			$result = $this->Branch_model->load_branch_from_id($id);
			$data = $this->Branch_model->data;
			if (!is_array($data))
				$this->load->view('system_error');
			else {
				$data['rellist']= $this->Relations_model->get_relations($id);
				if (!is_array($data['rellist']))
					$this->load->view('system_error');
				else {
					$this->load->model('Package_model');
					$data['relnew']= array( 'pkgs' => $this->Package_model->get_packages(),
											'branch_id' => $id);
					if (!is_array($data['relnew']))
						$this->load->view('system_error');
					else
						$this->load->view('branch/edit',$data);
				}
			}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Save the new information of the branch into the database
	 */
	function edit_branch($branch_id){
		if (is_numeric($branch_id) && $this->auth->has_rights_to("edit","bch", $branch_id))
		{
			$infos = array (
							'name' => $this->input->post('name'),
							'status' => $this->input->post('status'),
							'description' => $this->input->post('description')
			);
			if (!$this->Branch_model->save_branch($branch_id,$infos))
				$this->load->view('system_error');
     		else
     			$this->load->view('editOk');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	function list_relations($branch_id) {
		if (is_numeric($branch_id) && $this->auth->has_rights_to("list", "bch"))
		{
			$data['rellist']= $this->Relations_model->get_relations($branch_id);
			if (!is_array($data['rellist']))
				$this->load->view('system_error');
			else
				$this->load->view('branch/relations/list', $data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}

	
	/**
	 * Displays a dropdown containing the branches attached to a given package
	 */
	function list_branchs_dd($pkgid) {
		if (is_numeric($pkgid) && $this->auth->has_rights_to("list","bch"))
		{
			$data['branchlist'] = $this->Branch_model->get_branches($pkgid, null, null, array('status' => 'active'));
			if (!is_array($data['branchlist']))
				$this->load->view('system_error');
			else
				$this->load->view('branch/relations/list_branch_dd', $data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Deletes a relation
	 */
	function remove_rel($bch1, $reltype, $bch2) {
		$reltypes = array ('depends on', 'recommands', 'conflicts');
		if (is_numeric($bch1) && in_array($reltype, $reltypes) && is_numeric($bch2) && $this->auth->has_rights_to("edit", "bch", $bch1))
		{
			if (!$this->Relations_model->delete_relation($bch1, $reltype, $bch2))
				$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	
	function add_relation($bch1, $reltype, $bch2, $optdata = null) {
		$reltypes = array ('depends on', 'recommands', 'conflicts');
		if (is_numeric($bch1) && in_array($reltype, $reltypes) && is_numeric($bch2) && $this->auth->has_rights_to("edit", "bch", $bch1))
		{
			if (!$this->Relations_model->add_relation($bch1, $reltype, $bch2, $optdata))
				$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
}

?>
